/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.gastronomico.demo.repository.hibernate;

import com.gastronomico.demo.domain.Ubigeo;
import com.gastronomico.demo.repository.UbigeoRepository;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

/**
 *
 * @author mmuguerza
 */
@Repository //OBLIGATORIO
public class UbigeoRepositoryHibernate  extends HibernateDaoSupport
        implements UbigeoRepository{

    @Autowired
    public UbigeoRepositoryHibernate(SessionFactory sessionFactory) {
        setSessionFactory(sessionFactory);
    }

    @Override
    public Ubigeo obtenerUbigeo(String id) {
        return (Ubigeo) getSession().
                createQuery("from Ubigeo u "
                + "  where u.idubigeo = ?").
                setString(0, id).uniqueResult();
    }

    @Override
    public List<Ubigeo> obtenerDepartamentos() {
         return  getHibernateTemplate().find("from Ubigeo where prov='00' and dist='00' order by descripcion" );
    }

    @Override
    public List<Ubigeo> obtenerDistritos(String depv, String prov) {
        return  getHibernateTemplate().find("from Ubigeo where dept= '" +depv+"' and prov='"+prov+"' and dist<>'00' order by descripcion )" );
    }


    @Override
    public List<Ubigeo> obtenerProvincias(String depv) {
        return  getHibernateTemplate().find("from Ubigeo where dept= '" +depv+"' and prov<>'00' and dist='00'  order by descripcion )" );
    }

    public Ubigeo obtenerDepartamento(String dept) {
                return (Ubigeo) getSession().
                createQuery("from Ubigeo u "
                + "  where u.dept = ? and u.prov='00' and u.dist='00'" ).
                setString(0, dept).uniqueResult();
    }

    public Ubigeo obtenerDistrito(String prov, String dept, String dist) {
                return (Ubigeo) getSession().
                createQuery("from Ubigeo u "
                + "  where u.dept = '"+dept+"' and u.prov='"+prov+"' and u.dist='"+dist+"'" ).uniqueResult();

    }

    public Ubigeo obtenerProvincia(String prov, String dept) {
                return (Ubigeo) getSession().
                createQuery("from Ubigeo u "
                + "  where u.dept = '"+dept+"' and u.prov='"+prov+"' and u.dist='00'" ).uniqueResult();
    }

    

}
